package com.wy.business.market.message.services;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

import com.wy.business.market.market.pojos.TMarketPermissionStatus;
import com.wy.component.dataConvert.BeanUtil;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.services.AbstractBaseService;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * 项目名称：openportal
 * 类名称：ContactsService
 * 类描述：
 * 创建人：田松林
 * 创建时间：2016-1-8 下午2:56:29
 * 修改人：田松林
 * 修改时间：2016-1-8 下午2:56:29
 * 版本信息：@version 1.0
 * Copyright (c) 2016万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
public class ContactsService extends AbstractBaseService {
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Map> queryOrgTree() throws ErrorSqlException, FileNotFoundException, IOException{
		return BeanUtil.listToTree(dataBaseDao.queryForList("t_sys_orgInfo.list"), "id", "pid");
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> queryOrgUsers(String orgCode, String masterId, String friendUserId) throws ErrorSqlException{
		List<Map> list;
		if(StringUtils.isNotEmpty(friendUserId)){
			List<Object> params=new ArrayList<Object>();
			params.add(masterId);
			params.add(friendUserId);
			params.add(friendUserId);
			list = dataBaseDao.queryForList("t_sys_userinfo.queryOrgUserByFriendUserId", params);
		}else{
			List<Object> params=new ArrayList<Object>();
			params.add(masterId);
			params.add(masterId);
			params.add(orgCode);
			list = dataBaseDao.queryForList("t_sys_userinfo.queryOrgUser", params);
		}
		for(Map m : list){
			String slaveUserId = (String) m.get("USERID");
			List list1 = dataBaseDao.getHibernateTemplate().find("from TMarketPermissionStatus where userId = ? ", slaveUserId);
			if(list1!=null && list1.size()>0){
				TMarketPermissionStatus tMarketPermissionStatus = (TMarketPermissionStatus) list1.get(0);
//			0：对所有人公开  
//			1：对本部门公开
//			2：对子部门公开
//			3：仅对好友公开
//			4：不公开
				if(StringUtils.equals(tMarketPermissionStatus.getStatusVal(), "4")){
					m.put("SHOWTEL", "0");
				}else if(StringUtils.equals(m.get("ISFRIEND").toString(), "0") && StringUtils.equals(tMarketPermissionStatus.getStatusVal(), "3")){
					m.put("SHOWTEL", "0");
				}else if(!isChildOrg(masterId,slaveUserId) && StringUtils.equals(tMarketPermissionStatus.getStatusVal(), "2")){
					m.put("SHOWTEL", "0");
				}else if(!isCurrentOrg(masterId,slaveUserId) && StringUtils.equals(tMarketPermissionStatus.getStatusVal(), "1")){
					m.put("SHOWTEL", "0");
				}else if(StringUtils.equals(tMarketPermissionStatus.getStatusVal(), "0")){
					m.put("SHOWTEL", "1");
				}else
					m.put("SHOWTEL", "1");
			}else
				m.put("SHOWTEL", "1");
		}
		return firstCharacterSort(list);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private boolean isChildOrg(String masterUserId, String slaveUserId) throws ErrorSqlException {
		List params = new ArrayList();
		params.add(masterUserId);
		List list = dataBaseDao.queryForList("t_sys_userinfo.getOrgCodeByUserId",params);
		//当前登录人部门
		String masterOrgCode = (String) ((Map)list.get(0)).get("ORGCODE");
		
		params = new ArrayList();
		params.add(slaveUserId);
		list = dataBaseDao.queryForList("t_sys_userinfo.getOrgCodeByUserId",params);
		//好友部门
		String slaveOrgCode = (String) ((Map)list.get(0)).get("ORGCODE");
		
		params = new ArrayList();
		params.add(slaveOrgCode);
		params.add(masterOrgCode);
		params.add(slaveOrgCode);
		//将好友部门作为父部门,进行逻辑判断
		list = dataBaseDao.queryForList("t_sys_orgInfo.isChild",params);
		if(list==null || list.size()==0)
			return false;
		String flg = (String) ((Map)list.get(0)).get("FLG");
		if(StringUtils.equals(flg, "1"))
			return true;
		return false;
	}
	
	private boolean isCurrentOrg(String masterUserId, String slaveUserId) throws ErrorSqlException {
		List params = new ArrayList();
		params.add(masterUserId);
		List list = dataBaseDao.queryForList("t_sys_userinfo.getOrgCodeByUserId",params);
		String masterOrgCode = (String) ((Map)list.get(0)).get("ORGCODE");
		
		params = new ArrayList();
		params.add(slaveUserId);
		list = dataBaseDao.queryForList("t_sys_userinfo.getOrgCodeByUserId",params);
		String slaveOrgCode = (String) ((Map)list.get(0)).get("ORGCODE");
		
		if(StringUtils.equals(masterOrgCode, slaveOrgCode))
			return true;
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> queryContactSearch(String searchContent,String userId) throws ErrorSqlException{
		List<Object> params=new ArrayList<Object>();
		params.add(userId);
		params.add("%"+searchContent+"%");
		params.add(userId);
		return dataBaseDao.queryForList("t_sys_userinfo.searchContact",params);
	}
	
	public List<Map> firstCharacterSort(List<Map> list){
		HashSet<String> characterSet=new HashSet<String>();
		for (Map map : list) {
			if(map.get("PY")==null){
				characterSet.add("");
			}else{
				characterSet.add((String)map.get("PY"));
			}
		}
		List aaaList =new ArrayList();
		for (int i = 0; i < characterSet.size(); i++) {
			aaaList.add(characterSet.toArray()[i]);
		}
		Collections.sort(aaaList);
		List<Map> characterList=new ArrayList<Map>();
		for (int i = 0; i < aaaList.size(); i++) {
			Map<String, Object> characterMap=new TreeMap<String, Object>();
			List<Map> listset=new ArrayList<Map>();
			for (int j = 0; j < list.size(); j++) {
				if (StringUtils.equals((String)list.get(j).get("PY"), aaaList.toArray()[i].toString())) {
					listset.add(list.get(j));
				}
			}
			characterMap.put("character", aaaList.toArray()[i].toString());
			characterMap.put("list", listset);
			characterList.add(characterMap);
		}
		
		return characterList;
	}

}
